﻿
namespace Indeco.G24.Windows.Forms
{
	/// <summary>
	/// abstract VaiTro typed datagridview
	/// </summary>
	[System.ComponentModel.DesignerCategoryAttribute("code")]
	public abstract class VaiTroDataGridViewBase : System.Windows.Forms.UserControl
	{	
		/// <summary> 
		/// Thrown when the current Entity changed.
		/// </summary>
		public event System.EventHandler<VaiTroDataGridViewEventArgs> CurrentEntityChanged;
		
		#region Fields
				
		/// <summary> 
		/// the current entity.
		/// </summary>
		private Entities.VaiTro _currentVaiTro = null;
		//private Dictionary<string, string> _columnsHeaders = new Dictionary<string, string>();
		
		/// <summary> 
		/// the datagridview instance
		/// </summary>
		protected System.Windows.Forms.DataGridView uxVaiTroDataGridView;
		
		/// <summary> 
		/// the errorprovider
		/// </summary>
		protected System.Windows.Forms.ErrorProvider uxVaiTroErrorProvider;
		
		/// <summary> 
		/// the bindingsource
		/// </summary>
		protected System.Windows.Forms.BindingSource uxVaiTroBindingSource;
		
		
		/// <summary> 
		/// the DGV column associated with the IDVaiTro property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxIDVaiTroDataGridViewColumn;
		
		/// <summary> 
		/// the DGV column associated with the TenVaiTro property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxTenVaiTroDataGridViewColumn;
		
		/// <summary> 
		/// the DGV column associated with the MoTa property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxMoTaDataGridViewColumn;
		
		/// <summary> 
		/// the DGV column associated with the Xem property
		/// </summary>
		protected System.Windows.Forms.DataGridViewCheckBoxColumn uxXemDataGridViewColumn;
		
		/// <summary> 
		/// the DGV column associated with the Sua property
		/// </summary>
		protected System.Windows.Forms.DataGridViewCheckBoxColumn uxSuaDataGridViewColumn;
		
		/// <summary> 
		/// the DGV column associated with the Xoa property
		/// </summary>
		protected System.Windows.Forms.DataGridViewCheckBoxColumn uxXoaDataGridViewColumn;
		
		/// <summary> 
		/// the DGV column associated with the Taomoi property
		/// </summary>
		protected System.Windows.Forms.DataGridViewCheckBoxColumn uxTaomoiDataGridViewColumn;
		
		#endregion
					
		#region Combo list
		
		#endregion
		
		#region Main Datasource
		
		private Entities.TList<Entities.VaiTro> _VaiTroList;
				
		/// <summary> 
		/// The list of VaiTro to be displayed in the DGV.
		/// </summary>
		[System.ComponentModel.Category("Data")]
		public Entities.TList<Entities.VaiTro> VaiTroList
		{
			get {return this._VaiTroList;}
			set
			{
				this._VaiTroList = value;
				this.uxVaiTroBindingSource.DataSource = null;
				this.uxVaiTroBindingSource.DataSource = value;
				this.uxVaiTroDataGridView.DataSource = null;
				this.uxVaiTroDataGridView.DataSource = this.uxVaiTroBindingSource;				
				//this.uxVaiTroBindingSource.ListChanged += new System.ComponentModel.ListChangedEventHandler(uxVaiTroBindingSource_ListChanged);
				this.uxVaiTroBindingSource.CurrentItemChanged += new System.EventHandler(OnVaiTroBindingSourceCurrentItemChanged);
			}
		}
		
		private void OnVaiTroBindingSourceCurrentItemChanged(object sender, System.EventArgs e)
		{
			_currentVaiTro = uxVaiTroBindingSource.Current as Entities.VaiTro;
			
			if (_currentVaiTro != null)
			{
				_currentVaiTro.Validate();
			}
			//_VaiTro.Validate();
			OnCurrentEntityChanged();
		}

		//void uxVaiTroBindingSource_ListChanged(object sender, System.ComponentModel.ListChangedEventArgs e)
		//{
			//throw new Exception("The method or operation is not implemented.");
		//}		
		
		#endregion
		
		/// <summary>
		/// Gets the currently selected <c cref="Entities.VaiTro"/> instance.
		/// </summary>
		public Entities.VaiTro SelectedVaiTro
		{
			get {return this._currentVaiTro;}
		}
		
		#region other properties
		
		/// <summary>
		/// Gets or sets a value indicating whether you have provided your own data-management operations for the DataGridView control.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool VirtualMode
		{
			get {return this.uxVaiTroDataGridView.VirtualMode;}
			set
			{
				this.uxVaiTroDataGridView.VirtualMode = value;
			}
		}
		
		/// <summary>
		/// Gets or sets a value indicating whether the option to add rows is displayed to the user.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool AllowUserToAddRows
		{
			get {return this.uxVaiTroDataGridView.AllowUserToAddRows;}
			set {this.uxVaiTroDataGridView.AllowUserToAddRows = value;}
		}
		
		/// <summary>
		/// Gets or sets a value indicating whether the user is allowed to delete rows from the DataGridView.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool AllowUserToDeleteRows
		{
			get {return this.uxVaiTroDataGridView.AllowUserToDeleteRows;}
			set {this.uxVaiTroDataGridView.AllowUserToDeleteRows = value;}
		}
		
		/// <summary>
		/// Gets the <c cref="DataGridView"/> instance.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public System.Windows.Forms.DataGridView DataGridView
		{
			get {return this.uxVaiTroDataGridView;}
		}
		
		/// <summary>
		/// Gets a collection that contains all the columns in the control.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]		
		[System.ComponentModel.DesignerSerializationVisibilityAttribute(System.ComponentModel.DesignerSerializationVisibility.Visible)]
		//[System.ComponentModel.EditorAttribute("System.Windows.Forms.Design.DataGridViewColumnCollectionEditor, System.Design", typeof(System.Drawing.Design.UITypeEditor))] 
		public System.Windows.Forms.DataGridViewColumnCollection Columns
		{
			get { return this.uxVaiTroDataGridView.Columns; }
		}
		
		#endregion
		
		/// <summary> 
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.IContainer components = null;
	
		/// <summary> 
		/// Clean up any resources being used.
		/// </summary>
		/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
		protected override void Dispose(bool disposing)
		{
			if (disposing && (components != null))
			{
				components.Dispose();
			}
			base.Dispose(disposing);
		}
	
		#region Constructor
	
		/// <summary>
		/// Initializes a new instance of the <see cref="VaiTroDataGridViewBase"/> class.
		/// </summary>
		public VaiTroDataGridViewBase()
		{			
			this.components = new System.ComponentModel.Container();			
			
			this.uxVaiTroDataGridView = new System.Windows.Forms.DataGridView();
			this.uxVaiTroBindingSource = new System.Windows.Forms.BindingSource(this.components);
			this.uxVaiTroErrorProvider = new System.Windows.Forms.ErrorProvider(this.components);
			this.uxIDVaiTroDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.uxTenVaiTroDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.uxMoTaDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.uxXemDataGridViewColumn = new System.Windows.Forms.DataGridViewCheckBoxColumn();
			this.uxSuaDataGridViewColumn = new System.Windows.Forms.DataGridViewCheckBoxColumn();
			this.uxXoaDataGridViewColumn = new System.Windows.Forms.DataGridViewCheckBoxColumn();
			this.uxTaomoiDataGridViewColumn = new System.Windows.Forms.DataGridViewCheckBoxColumn();
			((System.ComponentModel.ISupportInitialize)(this.uxVaiTroDataGridView)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.uxVaiTroBindingSource)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.uxVaiTroErrorProvider)).BeginInit();
			this.SuspendLayout();
						
			// 
			// uxVaiTroErrorProvider
			// 
			this.uxVaiTroErrorProvider.ContainerControl = this;
			this.uxVaiTroErrorProvider.DataSource = this.uxVaiTroBindingSource;						
			// 
			// uxVaiTroDataGridView
			// 
			this.uxVaiTroDataGridView.AutoGenerateColumns = false;
			this.uxVaiTroDataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
			this.uxVaiTroDataGridView.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
		this.uxIDVaiTroDataGridViewColumn,
		this.uxTenVaiTroDataGridViewColumn,
		this.uxMoTaDataGridViewColumn,
		this.uxXemDataGridViewColumn,
		this.uxSuaDataGridViewColumn,
		this.uxXoaDataGridViewColumn,
		this.uxTaomoiDataGridViewColumn			});
			this.uxVaiTroDataGridView.Dock = System.Windows.Forms.DockStyle.Fill;
			this.uxVaiTroDataGridView.Location = new System.Drawing.Point(0, 0);
			this.uxVaiTroDataGridView.Name = "uxVaiTroDataGridView";
			this.uxVaiTroDataGridView.Size = new System.Drawing.Size(470, 300);
			this.uxVaiTroDataGridView.TabIndex = 0;	
			this.uxVaiTroDataGridView.BackgroundColor = System.Drawing.Color.WhiteSmoke;
			this.uxVaiTroDataGridView.EnableHeadersVisualStyles = false;
			this.uxVaiTroDataGridView.DataError += new System.Windows.Forms.DataGridViewDataErrorEventHandler(this.OnVaiTroDataGridViewDataError);
			this.uxVaiTroDataGridView.CellValueNeeded += new System.Windows.Forms.DataGridViewCellValueEventHandler(this.OnVaiTroDataGridViewCellValueNeeded);
			this.uxVaiTroDataGridView.CellValuePushed += new System.Windows.Forms.DataGridViewCellValueEventHandler(this.OnVaiTroDataGridViewCellValuePushed);
			
			//
			// uxIDVaiTroDataGridViewColumn
			//
			this.uxIDVaiTroDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxIDVaiTroDataGridViewColumn.DataPropertyName = "IDVaiTro";
			this.uxIDVaiTroDataGridViewColumn.HeaderText = "IDVaiTro";
			this.uxIDVaiTroDataGridViewColumn.Name = "uxIDVaiTroDataGridViewColumn";
			this.uxIDVaiTroDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxIDVaiTroDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxIDVaiTroDataGridViewColumn.ReadOnly = false;		
			//
			// uxTenVaiTroDataGridViewColumn
			//
			this.uxTenVaiTroDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxTenVaiTroDataGridViewColumn.DataPropertyName = "TenVaiTro";
			this.uxTenVaiTroDataGridViewColumn.HeaderText = "TenVaiTro";
			this.uxTenVaiTroDataGridViewColumn.Name = "uxTenVaiTroDataGridViewColumn";
			this.uxTenVaiTroDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxTenVaiTroDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxTenVaiTroDataGridViewColumn.ReadOnly = false;		
			//
			// uxMoTaDataGridViewColumn
			//
			this.uxMoTaDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxMoTaDataGridViewColumn.DataPropertyName = "MoTa";
			this.uxMoTaDataGridViewColumn.HeaderText = "MoTa";
			this.uxMoTaDataGridViewColumn.Name = "uxMoTaDataGridViewColumn";
			this.uxMoTaDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxMoTaDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxMoTaDataGridViewColumn.ReadOnly = false;		
			//
			// uxXemDataGridViewColumn
			//
			this.uxXemDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxXemDataGridViewColumn.DataPropertyName = "Xem";
			this.uxXemDataGridViewColumn.HeaderText = "Xem";
			this.uxXemDataGridViewColumn.Name = "uxXemDataGridViewColumn";
			this.uxXemDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxXemDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxXemDataGridViewColumn.ReadOnly = false;		
			//
			// uxSuaDataGridViewColumn
			//
			this.uxSuaDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxSuaDataGridViewColumn.DataPropertyName = "Sua";
			this.uxSuaDataGridViewColumn.HeaderText = "Sua";
			this.uxSuaDataGridViewColumn.Name = "uxSuaDataGridViewColumn";
			this.uxSuaDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxSuaDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxSuaDataGridViewColumn.ReadOnly = false;		
			//
			// uxXoaDataGridViewColumn
			//
			this.uxXoaDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxXoaDataGridViewColumn.DataPropertyName = "Xoa";
			this.uxXoaDataGridViewColumn.HeaderText = "Xoa";
			this.uxXoaDataGridViewColumn.Name = "uxXoaDataGridViewColumn";
			this.uxXoaDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxXoaDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxXoaDataGridViewColumn.ReadOnly = false;		
			//
			// uxTaomoiDataGridViewColumn
			//
			this.uxTaomoiDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxTaomoiDataGridViewColumn.DataPropertyName = "Taomoi";
			this.uxTaomoiDataGridViewColumn.HeaderText = "Taomoi";
			this.uxTaomoiDataGridViewColumn.Name = "uxTaomoiDataGridViewColumn";
			this.uxTaomoiDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxTaomoiDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxTaomoiDataGridViewColumn.ReadOnly = false;		
			
			// this
			this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
			this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
			this.Controls.Add(this.uxVaiTroDataGridView);
			this.Name = "VaiTroDataGridView";
			this.Size = new System.Drawing.Size(470, 300);
			((System.ComponentModel.ISupportInitialize)(this.uxVaiTroErrorProvider)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.uxVaiTroDataGridView)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.uxVaiTroBindingSource)).EndInit();
			this.ResumeLayout(false);
		}
		#endregion
				
		#region events
		
		/// <summary>
		/// Raised the CurrentEntityChanged event.
		/// </summary>
		protected void OnCurrentEntityChanged()
		{
			if (CurrentEntityChanged != null)
			{
				VaiTroDataGridViewEventArgs args = new VaiTroDataGridViewEventArgs();
				args.VaiTro = _currentVaiTro;
				CurrentEntityChanged(this, args); 
			}
		}
		
		#endregion
		
		/// <summary>
		/// Strongly typed event 
		/// </summary>
		public class VaiTroDataGridViewEventArgs : System.EventArgs
		{
			private Entities.VaiTro	_VaiTro;
	
			/// <summary>
			/// the  Entities.VaiTro instance.
			/// </summary>
			public Entities.VaiTro VaiTro
			{
				get { return _VaiTro; }
				set { _VaiTro = value; }
			}	
		}
		
		/// <summary>
		/// Handles the DataError event of the uxVaiTroDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewDataErrorEventArgs"/> instance containing the event data.</param>
		protected void OnVaiTroDataGridViewDataError(object sender, System.Windows.Forms.DataGridViewDataErrorEventArgs e)
		{
			System.Diagnostics.Debug.WriteLine(string.Format( "{0}, {1}", e.ColumnIndex, e.RowIndex));
		}
		
		/// <summary>
		/// Handles the CellValueNeeded event of the uxVaiTroDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewCellValueEventArgs"/> instance containing the event data.</param>
		protected void OnVaiTroDataGridViewCellValueNeeded(object sender, System.Windows.Forms.DataGridViewCellValueEventArgs e)
		{
			switch(uxVaiTroDataGridView.Columns[e.ColumnIndex].Name)
			{
					case "uxIDVaiTroDataGridViewColumn":
						e.Value = VaiTroList[e.RowIndex].IDVaiTro;
						break;
					case "uxTenVaiTroDataGridViewColumn":
						e.Value = VaiTroList[e.RowIndex].TenVaiTro;
						break;
					case "uxMoTaDataGridViewColumn":
						e.Value = VaiTroList[e.RowIndex].MoTa;
						break;
					case "uxXemDataGridViewColumn":
						e.Value = VaiTroList[e.RowIndex].Xem;
						break;
					case "uxSuaDataGridViewColumn":
						e.Value = VaiTroList[e.RowIndex].Sua;
						break;
					case "uxXoaDataGridViewColumn":
						e.Value = VaiTroList[e.RowIndex].Xoa;
						break;
					case "uxTaomoiDataGridViewColumn":
						e.Value = VaiTroList[e.RowIndex].Taomoi;
						break;
				default:
				break;
			}
		}
		
		/// <summary>
		/// Handles the CellValuePushed event of the uxVaiTroDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewCellValueEventArgs"/> instance containing the event data.</param>
		protected void OnVaiTroDataGridViewCellValuePushed(object sender, System.Windows.Forms.DataGridViewCellValueEventArgs e)
		{
			switch(uxVaiTroDataGridView.Columns[e.ColumnIndex].Name)
			{
					case "uxIDVaiTroDataGridViewColumn":
						VaiTroList[e.RowIndex].IDVaiTro = (System.Guid)e.Value;
						break;
					case "uxTenVaiTroDataGridViewColumn":
						VaiTroList[e.RowIndex].TenVaiTro = (System.String)e.Value;
						break;
					case "uxMoTaDataGridViewColumn":
						VaiTroList[e.RowIndex].MoTa = (System.String)e.Value;
						break;
					case "uxXemDataGridViewColumn":
						VaiTroList[e.RowIndex].Xem = (System.Boolean)e.Value;
						break;
					case "uxSuaDataGridViewColumn":
						VaiTroList[e.RowIndex].Sua = (System.Boolean)e.Value;
						break;
					case "uxXoaDataGridViewColumn":
						VaiTroList[e.RowIndex].Xoa = (System.Boolean)e.Value;
						break;
					case "uxTaomoiDataGridViewColumn":
						VaiTroList[e.RowIndex].Taomoi = (System.Boolean)e.Value;
						break;
				default:
				break;
			}
		}
	}
}
